import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

/// 显示默认的底部弹出框
showDefaultBottomSheet(BuildContext context,
        {String title,
        Widget child,
        bool isScrollControlled = false,
        bool isDismissible = true,
        Widget leading,
        double topMargin = 50,
        double bottomMargin = 52,
        bool hasClosed = true}) =>
    showModalBottomSheet(
        context: context,
        isScrollControlled: isScrollControlled,
        isDismissible: isDismissible,
        backgroundColor: Colors.transparent,
        builder: (BuildContext context) => DefaultBottomSheet(
              title: title,
              child: child,
              leading: leading,
              hasClose: hasClosed,
              topMargin: topMargin,
              bottomMargin: bottomMargin,
            ));

class DefaultBottomSheetLeadingButton extends StatelessWidget {
  final String title;
  final VoidCallback onTap;

  const DefaultBottomSheetLeadingButton({Key key, this.title, this.onTap})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return PressedOpacityButton(
      onPressed: onTap,
      disabledColor: Colors.transparent,
      padding: EdgeInsets.symmetric(
        horizontal: getDp(34),
        vertical: getDp(29),
      ),
      child: Text(
        title,
        style: TextStyle(
          color: onTap == null ? aceGrayColor : aceBlueColor,
          fontSize: getSp(18),
          fontWeight: FontWeight.w700,
          height: 22 / 18,
        ),
      ),
    );
  }
}

class DefaultBottomSheet extends StatelessWidget {
  final String title;
  final Widget child;
  final bool hasClose;
  final Widget leading;
  final String leftText;
  final double topMargin;
  final double bottomMargin;

  const DefaultBottomSheet(
      {Key key,
      this.title,
      this.leading,
      this.child,
      this.hasClose = true,
      this.topMargin = 0,
      this.bottomMargin = 52,
      this.leftText})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.transparency,
      child: SingleChildScrollView(
        child: Container(
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(getDp(30)),
                topRight: Radius.circular(getDp(30)),
              )),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Stack(
                children: [
                  if (leading != null)
                    Align(
                      alignment: Alignment.centerLeft,
                      child: leading,
                    ),
                  Align(
                    alignment: Alignment.center,
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Container(
                          margin: EdgeInsets.only(
                            top: getDp(10),
                            bottom: title == null ? 0 : getDp(10),
                          ),
                          width: getDp(57),
                          height: getDp(4),
                          decoration: BoxDecoration(
                            color: aceGrayColor,
                            borderRadius: BorderRadius.circular(getDp(5)),
                          ),
                        ),
                        if (title != null)
                          Padding(
                            padding: EdgeInsets.only(
                              top: getDp(5),
                            ),
                            child: Text(
                              '${title ?? ' '}',
                              style: TextStyle(
                                color: aceBlackColor,
                                fontSize: getSp(18),
                                fontWeight: FontWeight.w700,
                                height: 22 / 18,
                              ),
                            ),
                          ),
                      ],
                    ),
                  ),
                  if (child != null)
                    Container(
                        margin: EdgeInsets.only(top: getDp(topMargin)),
                        child: child),
                  if (hasClose == true)
                    Align(
                      alignment: Alignment.centerRight,
                      child: BottomSheetCloseButton(),
                    ),
                ],
              ),
              // if (child != null) child,
              SizedBox(
                height: MediaQuery.of(context).padding.bottom +
                    MediaQuery.of(context).viewInsets.bottom +
                    getDp(bottomMargin),
              )
            ],
          ),
        ),
      ),
    );
  }
}

class BottomSheetCloseButton extends StatelessWidget {
  final bool hasPadding;

  const BottomSheetCloseButton({Key key, this.hasPadding}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return PressedOpacityButton(
      padding: hasPadding == false
          ? EdgeInsets.zero
          : EdgeInsets.only(
              left: getDp(34),
              right: getDp(34),
              top: getDp(30),
              bottom: getDp(18),
            ),
      onPressed: () => Navigator.of(context).pop(),
      child: SvgPicture.asset(
        A.assetsImagesStyleClose,
        width: getDp(18),
        height: getDp(18),
        fit: BoxFit.contain,
      ),
    );
  }
}
